package com.web.portal.dao;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.web.portal.entity.Faculty;

@Repository(value = "facultyDao")
public class FacultyDaoImpl extends WebportalBaseDao<Faculty> implements
		FacultyDao {
	private final Logger LOG = Logger.getLogger(FacultyDaoImpl.class);

	@Override
	protected void populateCriteria(
			Map<? extends Enum<?>, Object> searchCriteria, Criteria criteria) {
		if (searchCriteria.containsKey(FacultySearchType.FIRST_NAME)) {
			criteria.add(Restrictions.eq("firstName",
					searchCriteria.get(FacultySearchType.FIRST_NAME)));
		}
		if (searchCriteria.containsKey(FacultySearchType.LAST_NAME)) {
			criteria.add(Restrictions.eq("lastName",
					searchCriteria.get(FacultySearchType.LAST_NAME)));
		}
		if (searchCriteria.containsKey(FacultySearchType.EMAIL)) {
			criteria.add(Restrictions.eq("email",
					searchCriteria.get(FacultySearchType.EMAIL)));
		}
		if (searchCriteria.containsKey(FacultySearchType.ID)) {
			criteria.add(Restrictions.eq("id",
					searchCriteria.get(FacultySearchType.ID)));
		}

	}

	public List<Faculty> listItems(Faculty faculty) {
		try {

			LOG.debug("Querying items from the list..........................");
			@SuppressWarnings("unchecked")
			List<Faculty> result = (List<Faculty>) getSession().createQuery(
					"from Faculty").list();
			if (result == null) {
				LOG.debug("Empty result found");
				result = Collections.emptyList();
			}
			/*
			 * returning list from Criteria query
			 */

			LOG.debug("Querying items is successfully executed...........................");
			LOG.debug("Returning result...............");
			return result;
		} catch (RuntimeException e) {
			LOG.debug("unable to populate query " + e);
			throw e;
		}
	}

}
